<?php

namespace IDock\Insurance\DAJIA\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\DAJIA\DAJIAEnums;
use IDock\Printer\Premium\PremiumCoveragePrinter;
use IDock\Printer\Premium\PremiumRelationPrinter;
use IDock\Printer\RenewalInquiryPrinter;
use IDock\Printer\VehiclePrinter;

class RenewalInquiryResponse extends AllDAJIAResponse
{
    public function printerInit(): Response
    {
        $renewalInquiryPrinter = new RenewalInquiryPrinter();

        $data = $this->getBody();
        $body = $data['body'] ?? '';
        $base = $data['body']['base'] ?? [];
        $base['crenewMrk'] = 1;
        if ($base['crenewMrk']) {
            $renewalInquiryPrinter->setRenewalFlg(true);
        } else {
            $this->printer = $renewalInquiryPrinter;
            return $this;
        }
        //车辆信息
        $vehicle = $body['vhl'] ?? [];
        $vehiclePrinter = new VehiclePrinter();
        $vehiclePrinter->vehicleName = $vehicle['cmodelNme'] ?? '';
        $vehiclePrinter->vehicleModelCode = $vehicle['cmodelCde'] ?? '';
        $vehiclePrinter->power = $vehicle['npower'] ?? '';
        $vehiclePrinter->vehiclePrice = $vehicle['nnewPurchaseValue'] ?? '';
        $vehiclePrinter->vehicleSeat = $vehicle['nseatNum'] ?? '';
        $vehiclePrinter->vehicleTonnage = $vehicle['ntonage'] ?? '';
        $vehiclePrinter->vehicleExhaust = $vehicle['nDisplacement'] ?? '';
        //车架号
        $renewalInquiryPrinter->vin = $vehicle['cfrmNo'] ?? '';
        //发动机号
        $renewalInquiryPrinter->engineNo = $vehicle['cengNo'] ?? '';
        $renewalInquiryPrinter->firstRegisterDate = $vehicle['cfstRegYmBar'] ?? '';//初登日期
//        $renewalInquiryPrinter->biPolicyNo = '';//上年商业险保单号

        //车主
        $ownerPrinter = new PremiumRelationPrinter();
        $owner = $body['vhlowner'] ?? '';
        $ownerPrinter->name = $owner['cownerNme'] ?? '';
        $ownerPrinter->mobile = $owner['cmobile'] ?? '';
        $ownerPrinter->address = $owner['cresidAddr'] ?? '';
        $ownerPrinter->certificateNo = $owner['ccertfCde'] ?? '';
        $ownerPrinter->certificateTypeCode = array_keys(DAJIAEnums::$certificateTypeCode, $owner['ccertfCls'] ?? '')[0] ?? '';
        $ownerPrinter->email = $owner['cemail'] ?? '';
        $ownerPrinter->typeCode = $ownerPrinter->certificateTypeCode == '01' ? '01' : '03';

        //投保人
        $applicant = $body['applicant'] ?? [];
        $applicantPrinter = new PremiumRelationPrinter();
        $applicantPrinter->name = $applicant['cclientNme'] ?? '';
        $applicantPrinter->mobile = $applicant['cmobile'] ?? '';
        $applicantPrinter->address = $applicant['cclntAddr'] ?? '';
        $applicantPrinter->certificateNo = $applicant['ccertfCde'] ?? '';
        $applicantPrinter->certificateTypeCode = array_keys(DAJIAEnums::$certificateTypeCode, $applicant['ccertfCls'] ?? '')[0] ?? '';
        $applicantPrinter->email = $applicant['cemail'] ?? '';
        $applicantPrinter->typeCode = $applicantPrinter->certificateTypeCode == '01' ? '01' : '03';

        //被保人
        $insuredPrinter = new PremiumRelationPrinter();
        $insuredPrinter->name = $owner['cownerNme'] ?? '';
        $insuredPrinter->mobile = $owner['cmobile'] ?? '';
        $insuredPrinter->address = $owner['cresidAddr'] ?? '';
        $insuredPrinter->certificateNo = $owner['ccertfCde'] ?? '';
        $insuredPrinter->certificateTypeCode = array_keys(DAJIAEnums::$certificateTypeCode, $owner['ccertfCls'] ?? '')[0] ?? '';
        $insuredPrinter->email = $owner['cemail'] ?? '';
        $insuredPrinter->typeCode = $insuredPrinter->certificateTypeCode == '01' ? '01' : '03';

        $renewalInquiryPrinter->setApplicantPrinter($applicantPrinter);
        $renewalInquiryPrinter->setInsuredPrinter($insuredPrinter);
        $renewalInquiryPrinter->setOwnerPrinter($ownerPrinter);
        $renewalInquiryPrinter->setVehiclePrinter($vehiclePrinter);

        $carShipTax = $body['vsTax'] ?? [];
        //交强险
        $renewalInquiryPrinter->ciEndData = $carShipTax['tLastSaliBgnDate'] ?? '';
        $renewalInquiryPrinter->ciPolicyNo = $carShipTax['tSaliAppDate'] ?? '';

        $coverageNo = $policyItem['ccvrgNo'] ?? '';
        $coverageList = $body['cvrgList'] ?? [];
        foreach ($coverageList as $policyItem){
            //商业险
            $renewalInquiryPrinter->biEndData = $policyItem['tendTm'] ?? '';
            $renewalInquiryPrinter->biStartData = $policyItem['tbgnTm'] ?? '';
            $renewalInquiryPrinter->biPolicyNo = $policyItem['cappNo'] ?? '';

            //商业险险种
            $coveragePrinter = new PremiumCoveragePrinter();
            $coverageNo = $policyItem['ccvrgNo'] ?? '';
            $coveragePrinter->coverageCode = array_search($coverageNo, DAJIAEnums::$coverageCode);

            if (!$coveragePrinter->coverageCode) {
                continue;
            }

            $coveragePrinter->coverageName = IDockEnums::$coverageCode[$coveragePrinter->coverageCode];
            $coveragePrinter->amount = $policyItem['namt'] ?? '';
            $coveragePrinter->unitAmount = $policyItem['nperAmt'] ?? '';
            $coveragePrinter->premium = $policyItem['nprm'] ?? '';

            $renewalInquiryPrinter->setCoveragePrinterList($coveragePrinter);
        }

        $this->printer = $renewalInquiryPrinter;

        return $this;
    }
}